import request from "@/utils/request";
import type {
  AdminUserListParams,
  UserList,
  AddAdminUserParams,
  UpdateAdminUserParams,
  GetUserRoleListApiResponse,
} from "./model/userModel";

const enum Api {
  getAdminUserList = "/admin/acl/user",
  addAdminUser = "/admin/acl/user/save",
  updateAdminUser = "/admin/acl/user/update",
  deleteAdminUser = "/admin/acl/user/remove",
  getRoleByUserId = "/admin/acl/user/toAssign",
  assignRole = "/admin/acl/user/doAssign",
}

/**
 * 获取管理用户分页列表
 * @param page 当前页
 * @param limit 每页多少条
 * @param username 用户名
 * @returns UserList 管理用户分页列表
 */
export const GetAdminUserListApi = ({
  page,
  limit,
  username,
}: AdminUserListParams) => {
  return request.get<any, UserList>(
    Api.getAdminUserList + `/${page}/${limit}`,
    {
      params: {
        username,
      },
    }
  );
};

/**
 * 添加管理用户
 * @param data AddAdminUserParams
 * @returns null
 */
export const AddAdminUserApi = (data: AddAdminUserParams) => {
  return request.post<any, null>(Api.addAdminUser, data);
};

/**
 * 修改管理用户
 * @param data UpdateAdminUserParams
 * @returns null
 */
export const UpdateAdminUserApi = (data: UpdateAdminUserParams) => {
  return request.put<any, null>(Api.updateAdminUser, data);
};

/**
 * 删除管理用户
 * @param id 用户id
 * @returns null
 */
export const DeleteAdminUserApi = (id: string) => {
  return request.delete<any, null>(Api.deleteAdminUser + `/${id}`);
};

/**
 * 根据用户获取角色数据
 * @param userId 用户id
 * @returns GetUserRoleListApiResponse
 */
export const GetRoleByUserIdApi = (userId: string) => {
  return request.get<any, GetUserRoleListApiResponse>(
    Api.getRoleByUserId + `/${userId}`
  );
};

/**
 * 根据用户分配角色
 * @param userId 用户id
 * @param roleId 角色id
 * @returns null
 */
export const AssignRoleApi = (userId: string, roleId: string[]) => {
  return request.post<any, null>(
    Api.assignRole,
    {},
    {
      params: {
        userId,
        roleId: roleId.join(","),
      },
    }
  );
};
